
import 'package:DiamondDiary/database/db_helper.dart';
import 'package:DiamondDiary/database/tag_info.dart';
import 'package:DiamondDiary/utils/route_helper.dart';
import 'package:DiamondDiary/widgets/dialogs.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:fluttertoast/fluttertoast.dart';

class TagPage extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    return TagWidget();
  }

}

class TagWidget extends State<TagPage>{

  var list = <Map<String,dynamic>>[];
  TextEditingController controller;
  var checkedList =<Map<String,dynamic>>[];

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 100),loadData);
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  void loadData() async{
    var data = await DBHelper().getTagInfo(orderBy: 'tag');
    if(data!=null) {
      setState(() {
        list.addAll(data);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('TAG'),
        actions: [
          InkWell(child: Center(child: Padding(
            child:Text('完成'),padding: EdgeInsets.fromLTRB(10, 5, 10, 5),),),
            onTap: ()=>RouteHelper.pop(context,checkedList),),
        ],
      ),
     floatingActionButton:  FloatingActionButton(child:Icon(Icons.add_circle,size: 30,),
       onPressed: addTag,
     ),
      body:ListView.separated(
            itemCount: list.length,
            itemBuilder: (context,index){
              var item = list[index];
              return Slidable(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(15, 8, 15, 8),
                  child:Row(children: [
                    Checkbox(value: checkedList.contains(item), onChanged: (checked)=>setState(()=>checked??false?checkedList.add(item):checkedList.remove(item))),
                    SizedBox(width: 10,),
                    Text(item['tag']??''),
                  ],),
                ),                actionPane:SlidableScrollActionPane(),
                secondaryActions: [
                  IconSlideAction(caption: '删除',color: Color(0xFFD35F5F),icon: Icons.delete,
                    onTap: ()=>Dialogs.showTipAlert(context, "删除本条",onConfirm:()=> removeItem(item,index)),),
                ],);
            },
            separatorBuilder: (context,index)=>Divider(height: 1,thickness: 1,color: Colors.black12,),
        ),
    );
  }

  void addTag(){
    if(controller == null)
      controller = TextEditingController();
    Dialogs.showInputDialog(context, '请输入', '', controller, (text) async{
      //保存
      var info = TagInfo();
      info.createTime = DateTime.now().millisecondsSinceEpoch;
      info.tag = text.toString().trim();
      int cnt = await DBHelper().saveTag(info);
      print('blen $cnt');
      if(cnt>0) {
        list.add(info.toJson());
        setState(() {});
      }else{
        Fluttertoast.showToast(msg: 'tag 已存在');
      }
    });
  }

  void removeItem(item,index)async{
    DBHelper().removeTag(item['tag']);
    list.removeAt(index);
    setState(() {

    });
  }

}